package com.dqp.cslggroup.Widget;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.view.View;
import android.widget.RemoteViews;

import com.dqp.cslggroup.R;
import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.UI.SplashActivity;
import com.dqp.cslggroup.Util.CompressedBitmap;
import com.dqp.cslggroup.Util.GetTimeUtil;
import com.dqp.cslggroup.bean.event;
import com.dqp.cslggroup.greendao.eventDao;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


public class MyEvent extends AppWidgetProvider {

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_event);
        event event = initView();
        if (event.getDate()!=null) {
            Date d = new Date();
            @SuppressLint("SimpleDateFormat") DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String Dates = df.format(d);
            int sel = event.getSelect();
            int count;
            String date = event.getDate();
            String name = event.getName();
            if (name == null) name = "CSLG助手诞生";
            if (date == null) date = "2019-09-06";

            if (sel == 0) {
                views.setViewVisibility(R.id.event_di, View.VISIBLE);
                views.setTextViewText(R.id.we_content, name);
                count = GetTimeUtil.addDates(date, Dates);
                views.setTextViewText(R.id.we_count, count + "");
            } else if (sel == 1) {
                count = GetTimeUtil.addDates(Dates, date);
                if (count > 0) {
                    views.setTextViewText(R.id.we_content, "距离" + name + "的生日还有");
                } else {
                    views.setTextViewText(R.id.we_content, name + "的生日已过去");
                    count = GetTimeUtil.addDates(date, Dates);
                }
                views.setTextViewText(R.id.we_count, count + "");
            } else {
                count = GetTimeUtil.addDates(Dates, date);
                if (count > 0) {
                    views.setTextViewText(R.id.we_content, name + "仅剩");
                } else {
                    views.setTextViewText(R.id.we_content, name + "已过去");
                    count = GetTimeUtil.addDates(date, Dates);
                }
                views.setTextViewText(R.id.we_count, count + "");
            }
            views.setTextViewText(R.id.we_date, date);
            views.setTextViewText(R.id.we_msg, event.getBz());
            if (event.getBg() != null) {
                Matrix matrix = new Matrix();
                matrix.setScale(0.5f, 0.5f);
                if (CompressedBitmap.fileExist(event.getBg())) {
                    Bitmap bitmap = BitmapFactory.decodeFile(event.getBg());
                    if (bitmap != null) {
                        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                        views.setImageViewBitmap(R.id.we_img, bitmap);
                        views.setImageViewBitmap(R.id.we_imgs, bitmap);
                    }
                }
            }
        }

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        Intent intent = new Intent(context, SplashActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.my_event);
        remoteViews.setOnClickPendingIntent(R.id.event_we, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
    }


    @SuppressLint("SetTextI18n")
    private static event initView() {
        event event=new event();
        List<event> eventList = MyApplication.getDaoInstant().getEventDao().queryBuilder()
                .where(eventDao.Properties.Zd.eq(1))
                .build()
                .list();
        if (eventList.size() > 0) {
            event = eventList.get(0);
        }else {
            eventList=MyApplication.getDaoInstant().getEventDao().loadAll();
            if (eventList.size()>0)
                event=eventList.get(0);
        }
        return event;
    }


}

